#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

struct ListNode 
{
	int val;
	struct ListNode *next;
};

struct ListNode* BuySList(int n)
{
	struct ListNode* ptr = (struct ListNode*)malloc(sizeof(struct ListNode));
	if (ptr == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}
	ptr->next = NULL;
	ptr->val = n;
	return ptr;
}
struct ListNode* CreateSL(int* arr,int x)
{
	assert(0 != x);
	struct ListNode*ps = BuySList(arr[0]);
	struct ListNode* ptr = ps;
	for (int i = 1; i < x; i++)
	{
		ps->next = BuySList(arr[i]);
		ps = ps->next;
	}
	return ptr;
}

struct ListNode* removeElements(struct ListNode* head, int val)
{
	struct ListNode* cur = head;
	struct ListNode* plist = NULL;
	struct ListNode* newhead = NULL;
	while (cur)
	{
		if (cur->val != val)
		{
			if (plist == NULL)
			{
				plist = newhead = cur;
				cur = cur->next;
			}
			else
			{
				plist->next = cur;
				plist = plist->next;
				cur = cur->next;
			}
		}
		else
		{
			struct ListNode* ptr = cur->next;
			plist->next = ptr;
			plist = plist->next;
			free(cur);
			cur = ptr;
		}
	}
	plist->next = NULL;
	return newhead;
}

int main()
{
	int arr[] = { 1, 2, 6, 3, 4, 5, 6 };
	struct ListNode* src = CreateSL(arr, sizeof(arr) / sizeof(arr[0]));
		

	struct ListNode* ret = removeElements(src,6);


	return 0;
}